<?php  
class Modelrichsnippetsrichsnippetsproduct extends Model {
	
	public function GetProductDetails() { 
		
		if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . DB_PREFIX . "product_gr_details' "))==1){
			
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_gr_details WHERE 1");
		    return $query->row;
			
		} else{
			return false;
		}
		
	} 
	
	public function addProductDetails($data) {  
	
		$payment_methods=implode(",",$data['payment_methods']);
		$delivery_methods=implode(",",$data['delivery_methods']);
	   
		if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . DB_PREFIX . "product_gr_details' "))==1){  
 
		   $alter_product_gr_details = "ALTER TABLE " . DB_PREFIX . "product_gr_details MODIFY COLUMN get_product_gr_status TEXT ";
		   $this->db->query($alter_product_gr_details);
		    
		   $this->db->query("INSERT INTO `" . DB_PREFIX . "product_gr_details` SET  
		                  business_function = '" . $this->db->escape($data['business_function']) . "', 
		                  payment_methods = '" . $this->db->escape($payment_methods) . "', 
		                  delivery_methods = '" . $this->db->escape($delivery_methods) . "', 
		                  get_product_gr_status = 'PGRpdiBzdHlsZT0nbWFyZ2luOmF1dG87dGV4dC1hbGlnbjpyaWdodDsnPjxhIHN0eWxlPSd0ZXh0LWRlY29yYXRpb246bm9uZTsnIHRhcmdldD0nX2JsYW5rJyBocmVmPSdodHRwOi8vd3d3LnRyYW5zcGFjaWZpYy1zb2Z0d2FyZS5jb20vb3BlbmNhcnQtZWNvbW1lcmNlLWV4dGVuc2lvbi5waHAnPnNlbWFudGljIHRhZ3MgcG93ZXJlZCBieSBUcmFuc1BhY2lmaWMgU29mdHdhcmU8L2E+PC9kaXY+', 
		                  status = '" . (int)$data['status'] . "', 
		                  date_added = NOW()");
		   
		   $sql=mysql_query("select gr_status from " . DB_PREFIX . "product WHERE 1");
             if (!$sql){ 
					 $add_gr_status_product = "ALTER TABLE " . DB_PREFIX . "product ADD gr_status TINYINT( 1 ) NOT NULL DEFAULT '1'";
     				 $this->db->query($add_gr_status_product);  
		     } 
      
		    
		}else{   
			
		$create_product_details = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "product_gr_details` (
		`id` int(11) NOT NULL auto_increment, 
		`business_function` varchar(100) collate utf8_bin NOT NULL default '',
        `payment_methods` varchar(100) collate utf8_bin NOT NULL default '',
        `delivery_methods` varchar(100) collate utf8_bin NOT NULL default '',
        `get_product_gr_status` TEXT collate utf8_bin NOT NULL default '',
        `status` int(1) NOT NULL default '0', 
        `date_added` datetime default NULL,
        PRIMARY KEY  (`id`)) 
        ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
	 
		$this->db->query($create_product_details); 
		
		 	 $sql=mysql_query("select gr_status from " . DB_PREFIX . "product WHERE 1");
             if (!$sql){ 
					 $add_gr_status_product = "ALTER TABLE " . DB_PREFIX . "product ADD gr_status TINYINT( 1 ) NOT NULL DEFAULT '1'";
     				 $this->db->query($add_gr_status_product);  
		     } 
		
		$this->db->query("INSERT INTO `" . DB_PREFIX . "product_gr_details` SET   
		                  business_function = '" . $this->db->escape($data['business_function']) . "', 
		                  payment_methods = '" . $this->db->escape($payment_methods) . "', 
		                  delivery_methods = '" . $this->db->escape($delivery_methods) . "',  
		                  get_product_gr_status = 'PGRpdiBzdHlsZT0nbWFyZ2luOmF1dG87dGV4dC1hbGlnbjpyaWdodDsnPjxhIHN0eWxlPSd0ZXh0LWRlY29yYXRpb246bm9uZTsnIHRhcmdldD0nX2JsYW5rJyBocmVmPSdodHRwOi8vd3d3LnRyYW5zcGFjaWZpYy1zb2Z0d2FyZS5jb20vb3BlbmNhcnQtZWNvbW1lcmNlLWV4dGVuc2lvbi5waHAnPnNlbWFudGljIHRhZ3MgcG93ZXJlZCBieSBUcmFuc1BhY2lmaWMgU29mdHdhcmU8L2E+PC9kaXY+',
		                  status = '" . (int)$data['status'] . "', 
		                  date_added = NOW()");
		  
		}
		  
	}
	
	public function editProductDetails($edit_id, $data) {
		
        $alter_product_gr_details = "ALTER TABLE " . DB_PREFIX . "product_gr_details MODIFY COLUMN get_product_gr_status TEXT ";
		$this->db->query($alter_product_gr_details);	
		
		if(isset($data['payment_methods'])){ 
		 	$payment_methods=implode(",",$data['payment_methods']);
		}
		if(isset($data['delivery_methods'])){
		$delivery_methods=implode(",",$data['delivery_methods']);
		}
		$this->db->query("UPDATE " . DB_PREFIX . "product_gr_details SET  
		                  business_function = '" . $this->db->escape($data['business_function']) . "', 
		                  payment_methods = '" . $this->db->escape($payment_methods) . "', 
		                  delivery_methods = '" . $this->db->escape($delivery_methods) . "', 
		                  get_product_gr_status = 'PGRpdiBzdHlsZT0nbWFyZ2luOmF1dG87dGV4dC1hbGlnbjpyaWdodDsnPjxhIHN0eWxlPSd0ZXh0LWRlY29yYXRpb246bm9uZTsnIHRhcmdldD0nX2JsYW5rJyBocmVmPSdodHRwOi8vd3d3LnRyYW5zcGFjaWZpYy1zb2Z0d2FyZS5jb20vb3BlbmNhcnQtZWNvbW1lcmNlLWV4dGVuc2lvbi5waHAnPnNlbWFudGljIHRhZ3MgcG93ZXJlZCBieSBUcmFuc1BhY2lmaWMgU29mdHdhcmU8L2E+PC9kaXY+',
		                  status = '" . (int)$data['status'] . "', 
		                  date_added = NOW()
		                  WHERE id = '" . (int)$edit_id . "'");
		  
	}
	 
	public function deleteProductDetails($edit_id) { 
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_gr_details WHERE id = '" . (int)$edit_id . "'");  
	}	
    
	public function GetProductDetailsbyId($id) { 
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_gr_details WHERE id='".$id."'");
		return $query->row;
	} 
} 
?>